import Vue from 'vue'
import router from './router'
import store from './store'
import storage from 'store'

import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

import { setDocumentTitle } from '@/utils/domUtils'
import { ACCESS_TOKEN } from '@/store/mutation-types'

NProgress.configure({ showSpinner: false }) // NProgress配置

const whiteList = ['Exception403', 'Exception404', 'Exception500']


router.beforeEach((to, from, next) => {
  NProgress.start()
  to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title}`))

  // const token = storage.get(ACCESS_TOKEN)
  next()

  /* if (token) {
    next()
  } else {
    if (whiteList.includes(to.name)) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next()
      NProgress.done() // 如果当前页面是登录页面，将不会触发路由后置钩子，所以需要手动处理它
    }
  } */


  // store.commit('SET_ROUTERS', [])
  // next()
  // console.log(store.getters.addRouters)
  // if(store.getters.addRouters.length === 0){
  //   store.dispatch('GenerateRoutes')
  //   .then(data => {
  //     // 动态添加可访问路由表
  //     router.addRoutes(data)

  //     // console.log(_.cloneDeep(data))
  //     next({...to, replace: true})
  //   })
  // } else {
  //   next()
  // }
})

router.afterEach(() => {
  NProgress.done()
})